package com.power.sifa.util;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;


/**
 * @ClassName PdfReadUtil
 * @Author xiongzw
 * @Date 2023/10/16 9:58
 * @Description
 * @Version: 1.0
 **/
@Slf4j
public class RemoteFileUtil {

    /**
     * 获取远程url文件 得到文件流
     *
     * @param filePath
     * @return
     */
    public static InputStream getFileInputStream(String filePath) throws IOException {
        int HttpResult; // 服务器返回的状态
        InputStream inputStream = null;
        try {
            filePath = filePath.replace("10.19.196.63", "172.21.61.230:6098");
            URL url = new URL(filePath); // 创建URL
            URLConnection urlconn = url.openConnection(); // 试图连接并取得返回状态码
            urlconn.connect();
            HttpURLConnection httpconn = (HttpURLConnection) urlconn;
            HttpResult = httpconn.getResponseCode();
            if (HttpResult != HttpURLConnection.HTTP_OK) {
                log.error("获取文件失败，无法连接到文件资源");
            } else {
                int filesize = urlconn.getContentLength(); // 取数据长度
                log.info("取数据长度:{}", filesize);
                inputStream = urlconn.getInputStream();
                //如果这里只需要返回stream，则直接返回 不需要转byte[]
                return urlconn.getInputStream();
            }
        } catch (IOException e) {
            e.printStackTrace();
            log.error("获取文件异常，e:{}", e);
        }
        return inputStream;
    }

    /**
     * 获取远程文件 得到字节数组
     *
     * @param filePath
     * @return
     */
    public static byte[] getFile(String filePath) throws IOException {
        int HttpResult; // 服务器返回的状态
        byte[] bytes = new byte[204800]; //设置数组大小
        InputStream inputStream = null;
        ByteArrayOutputStream swapStream = null;
        try {
            filePath = filePath.replace("10.19.196.63", "172.21.61.230:6098");
            URL url = new URL(filePath); // 创建URL
            URLConnection urlconn = url.openConnection(); // 试图连接并取得返回状态码
            urlconn.connect();
            HttpURLConnection httpconn = (HttpURLConnection) urlconn;
            HttpResult = httpconn.getResponseCode();
            if (HttpResult != HttpURLConnection.HTTP_OK) {
                log.error("获取文件失败，无法连接到文件资源");
            } else {
                int filesize = urlconn.getContentLength(); // 取数据长度
                log.info("取数据长度:{}", filesize);
                urlconn.getInputStream();
                inputStream = urlconn.getInputStream();
                //如果这里只需要返回stream，则直接返回 不需要转byte[]

                swapStream = new ByteArrayOutputStream();
                int ch;
                while ((ch = inputStream.read()) != -1) {
                    swapStream.write(ch);
                }
                bytes = swapStream.toByteArray();
            }
            log.info("文件大小，length:{}", bytes.length);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("获取文件异常，e:{}", e);
        } finally {
            inputStream.close();
            swapStream.close();
        }
        return bytes;
    }
}
